<template>
  <el-dialog
    :title="!dataForm.id ? '新增搜索引擎' : '修改搜索引擎'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="引擎名称" prop="engineName">
        <el-input v-model="dataForm.engineName" placeholder="请输入引擎名称"></el-input>
      </el-form-item>
      <el-form-item label="引擎类型" prop="engineType">
        <el-select v-model="dataForm.engineType" placeholder="请选择引擎类型" style="width: 100%">
          <el-option label="ElasticSearch" value="ELASTICSEARCH"></el-option>
          <el-option label="Solr" value="SOLR"></el-option>
          <el-option label="Algolia" value="ALGOLIA"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="端点地址" prop="endpoint">
        <el-input v-model="dataForm.endpoint" placeholder="请输入引擎端点地址"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择状态" style="width: 100%">
          <el-option label="启用" value="ENABLED"></el-option>
          <el-option label="禁用" value="DISABLED"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        engineName: '',
        engineType: '',
        endpoint: '',
        status: ''
      },
      dataRule: {
        engineName: [
          { required: true, message: '引擎名称不能为空', trigger: 'blur' }
        ],
        engineType: [
          { required: true, message: '请选择引擎类型', trigger: 'change' }
        ],
        endpoint: [
          { required: true, message: '端点地址不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/searchengine/searchengine/queryWormholeSearchEngine`),
            method: 'post',
            data: this.$http.adornData({
              "id": id
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.engineName = data.wmhSearchEngine.engineName
              this.dataForm.engineType = data.wmhSearchEngine.engineType
              this.dataForm.endpoint = data.wmhSearchEngine.endpoint
              this.dataForm.status = data.wmhSearchEngine.status
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/searchengine/searchengine/${!this.dataForm.id ? 'createWormholeSearchEngine' : 'updateWormholeSearchEngine'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'engineName': this.dataForm.engineName,
              'engineType': this.dataForm.engineType,
              'endpoint': this.dataForm.endpoint,
              'status': this.dataForm.status
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
